//
//  CustomPresentationVc.m
//  OnionHome
//
//  Created by fengjie on 2019/9/13.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "CustomPresentationVc.h"
#import "PresentationObject.h"

@interface CustomPresentationVc ()
/**<#属性#>*/
@property (nonatomic,strong) UIButton * deletBtn;
/**<#属性#>*/
@property (nonatomic,strong)UIPageControl*pageCtl;
@end

@implementation CustomPresentationVc
//presentationTransitionWillBegin 是在呈现过渡即将开始的时候被调用的。我们在这个方法中把半透明黑色背景 View 加入到 containerView 中，并且做一个 alpha 从0到1的渐变过渡动画。
- (void) presentationTransitionWillBegin{
    
    //使用UIVisualEffectView实现模糊效果
    UIBlurEffect *blur  = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    _visualView = [[UIVisualEffectView alloc]initWithEffect:blur];
    _visualView.frame = self.containerView.bounds;
    DLog(@"%f",[PresentationObject sharedPresentationObject].alpha_bgView);
    CGFloat alpha_bgView = [PresentationObject sharedPresentationObject].alpha_bgView;
    _visualView.alpha = alpha_bgView == 0 ? 0.6 : alpha_bgView;//0.6
    if (alpha_bgView == -1) {
        _visualView.alpha = 0;
    }
    _visualView.backgroundColor = [UIColor blackColor];
    [_visualView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissController)]];
    [self.containerView addSubview:_visualView];
    
    self.deletBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.deletBtn setBackgroundImage:IMAGE_NAMED(@"X") forState:UIControlStateNormal];
    self.deletBtn.frame = CGRectMake(16, 12, 20, 20);
    kWeakSelf(weakSelf);
    [self.deletBtn addActionHandler:^{
        [weakSelf dismissController];
    }];
    
    [self.containerView addSubview: self.deletBtn];
    
    [self.containerView addSubview:self.pageCtl];
    [self.pageCtl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(12);
    }];
    
    self.deletBtn.hidden = ![PresentationObject sharedPresentationObject].isShowTopView;
    self.pageCtl.hidden = ![PresentationObject sharedPresentationObject].isShowTopView;

    [self setNotification];
    
}
-(void)setNotification{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction:) name:K_ChangePageControl object:nil];
}
-(void)notificationAction:(NSNotification *)not{
//    DLog(@"%@",not);
    NSNumber * index = not.object[@"index"];
    self.pageCtl.currentPage = index.integerValue;
    
}
-(UIPageControl*)pageCtl{
    if(!_pageCtl){
        _pageCtl=[[UIPageControl alloc] initWithFrame:CGRectZero];
        _pageCtl.numberOfPages = 5;
        _pageCtl.pageIndicatorTintColor = UIColorFromRGB(0xD1D2D1);
        _pageCtl.currentPageIndicatorTintColor = KWhiteColor;
    }
    return _pageCtl;
}
-(void)dismissController{
    BlockSafeRun(self.canceBlock);
    [PresentationObject sharedPresentationObject].isShowTopView = NO;
    [PresentationObject sharedPresentationObject].alpha_bgView = 0.6;
    [PresentationObject sharedPresentationObject].subViewFrame = CGRectMake(0, KScreenHeight - 240, KScreenWidth, 240);

    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
//presentationTransitionDidEnd: 是在呈现过渡结束时被调用的，并且该方法提供一个布尔变量来判断过渡效果是否完成。在我们的例子中，我们可以使用它在过渡效果已结束但没有完成时移除半透明的黑色背景 View。
-(void)presentationTransitionDidEnd:(BOOL)completed {
    
    // 如果呈现没有完成，那就移除背景 View
    if (!completed) {
        [_visualView removeFromSuperview];
    }
}
//以上就涵盖了我们的背景 View 的呈现部分，我们现在需要给它添加淡出动画并且在它消失后移除它。正如你预料的那样，dismissalTransitionWillBegin 正是我们把它的 alpha 重新设回0的地方。
-(void)dismissalTransitionWillBegin {
    _visualView.alpha = 0.0;
}
//我们还需要在消失完成后移除背景 View。做法与上面 presentationTransitionDidEnd: 类似，我们重载 dismissalTransitionDidEnd: 方法
-(void)dismissalTransitionDidEnd:(BOOL)completed{
    if (completed) {
        [_visualView removeFromSuperview];
    }
}
//还有最后一个方法需要重载。在我们的自定义呈现中，被呈现的 view 并没有完全完全填充整个屏幕，而是很小的一个矩形。被呈现的 view 的过渡动画之后的最终位置，是由 UIPresentationViewController 来负责定义的。我们重载 frameOfPresentedViewInContainerView 方法来定义这个最终位置
- (CGRect)frameOfPresentedViewInContainerView
{
    CGFloat windowH = [UIScreen mainScreen].bounds.size.height;
    CGFloat windowW = [UIScreen mainScreen].bounds.size.width;
    //    self.presentedView.frame = CGRectMake(0, windowH - 260, windowW, 260);
    self.presentedView.frame = [PresentationObject sharedPresentationObject].subViewFrame;
    if ([NSStringFromCGRect(self.presentedView.frame) isEqualToString:NSStringFromCGRect(CGRectZero)]) {
        self.presentedView.frame = CGRectMake(0, windowH - 240, windowW, 240);
    }
    return self.presentedView.frame;
}

@end
